library(conflicted)
library(eurostat)
library(gets)
library(tidyverse)
library(janitor)
library(kableExtra)
library(plotly)

conflict_prefer("filter", "dplyr")
conflict_prefer("as.zoo.data.frame", "zoo")
conflict_prefer("select", "dplyr")
options(bitmapType = 'cairo') # for ggplotly

\[ TOTS = TOTD \]

\[TOTS = B + Y\]

\[TOTD = CP + CO + J + A + JL\]

Variables

Code Eurostat NAM Variable Full Name
B1GQ Y Gross domestic product at market prices
B1G YF Value added, gross
P6 A Exports of goods and services
P7 B Imports of goods and services
P5G J Gross capital formation
P3 CP + CO Final consumption expenditure
P3_S13 CO Final consumption expenditure of general government
P31_S13 Individual consumption expenditure of general government
P32_S13 Collective consumption expenditure of general government
P31_S14_S15 CP Household and NPISH final consumption expenditure
P31_S14 Final consumption expenditure of households
P31_S15 Final consumption expenditure of NPISH
YA1 JL Statistical discrepancy (production approach)
YA0 JL Statistical discrepancy (expenditure approach)
YA2 JL Statistical discrepancy (income approach)

Simple Aggregate Modelling

Input Data

q_GDP <- c("namq_10_gdp", # Hauptkomponenten
           "namq_10_fcs", # Konsum
           "namq_10_exi" # Exporte
)
geo_subset = "AT"
saving <- FALSE
overall <- tibble()
for(var in q_GDP[[1]]){
  full <- get_eurostat(id = var)
  
  full %>% 
    filter(geo == geo_subset) %>% 
    label_eurostat() -> intermed
  
  
  overall <- bind_rows(overall, intermed)
}
## Table namq_10_gdp cached at C:\Users\morit\AppData\Local\Temp\RtmpgtG8Ae/eurostat/namq_10_gdp_date_code_FF.rds

Decisions to be made

Which adjustment do we use?

1 Calendar adjusted data, not seasonally adjusted data
2 Unadjusted data (i.e. neither seasonally adjusted nor calendar adjusted data) 3 Seasonally and calendar adjusted data

Initially, I would have used Calendar adjusted data, not seasonally adjusted data to be able to forecast seasonal cycles as well. However, this is not available for all variables, so we now use “Seasonally and calendar adjusted data”

#s_adj_choice <- "Calendar adjusted data, not seasonally adjusted data"
s_adj_choice <- "Seasonally and calendar adjusted data"

Which unit do we use?

Initially, we will use Current Prices in Mio. €

unit_choice <- "Current prices, million euro"

Initial Plotting

overall %>% 
  filter(s_adj == s_adj_choice, unit == unit_choice) %>% 
  # filter(na_item %in% c("Final consumption expenditure", "Final consumption expenditure of general government", 
  #          "Household and NPISH final consumption expenditure")) %>% 
  filter(grepl("expenditure",na_item, ignore.case = TRUE), !grepl("gross", na_item, ignore.case = TRUE)) %>% 
  select(na_item, time, values) %>% 
  
  mutate(group = case_when(na_item == "Individual consumption expenditure of general government"~"gov",
                           na_item == "Collective consumption expenditure of general government"~"gov",
                           na_item == "Final consumption expenditure of households"~"hh",
                           na_item == "Final consumption expenditure of NPISH"~"hh",
                           TRUE~na_item)) %>% 
  
  group_by(group, time) %>% 
  summarise(values= sum(values),.groups = "drop") %>% 
  
  ggplot(aes(x = time, y = values, color = group)) + 
  geom_line() + 
  #facet_wrap(~na_item, scales = "fixed")+
  theme_minimal()+
  geom_hline(aes(yintercept = 0))+
  theme(legend.position = "bottom")

overall %>% 
  filter(s_adj == s_adj_choice, unit == unit_choice) %>% 
  filter(na_item %in% c("Final consumption expenditure", "Final consumption expenditure of general government",
                        "Household and NPISH final consumption expenditure")) %>%
  #filter(grepl("expenditure",na_item, ignore.case = TRUE), !grepl("gross", na_item, ignore.case = TRUE)) %>% 
  select(na_item, time, values) %>% 
  pivot_wider(time, na_item, values_from = values) %>%
  clean_names() %>% 
  
  mutate(test = household_and_npish_final_consumption_expenditure + final_consumption_expenditure_of_general_government,
         test_diff = final_consumption_expenditure - test)  %>% 
  select(time, test, final_consumption_expenditure) %>% 
  pivot_longer(-time) %>% 
  
  ggplot(aes(x = time, y = value, color = name)) + 
  geom_line() + 
  #facet_wrap(~name, scales = "free")+
  theme_minimal()+
  geom_hline(aes(yintercept = 0))+
  theme(legend.position = "bottom") 

Subsetting the right variables

overall %>% 
  filter(s_adj == s_adj_choice) %>% 
  filter(unit == unit_choice) %>% 
  filter(na_item %in% c("Final consumption expenditure of general government",
                        #"Final consumption expenditure", 
                        "Household and NPISH final consumption expenditure",
                        "Gross domestic product at market prices",
                        "Gross capital formation",
                        "Exports of goods and services", "Imports of goods and services",
                        #"Statistical discrepancy (production approach)", 
                        #"Statistical discrepancy (income approach)",
                        "Statistical discrepancy (expenditure approach)")) -> analysis_data_raw

analysis_data_raw %>% 
  ggplot(aes(x = time, y = values, color = na_item)) + 
  geom_line() + 
  facet_wrap(~na_item, scales = "free_y")+
  theme_minimal()+
  theme(legend.position = "none")

# c("Value added, gross", "Gross capital formation", "Final consumption expenditure", "Final consumption expenditure of general government", "Individual consumption expenditure of general government", "Collective consumption expenditure of general government", "Household and NPISH final consumption expenditure", "Final consumption expenditure of households", "Final consumption expenditure of NPISH", NA)
analysis_data_raw %>% 
  select(-unit, -s_adj) %>% 
  pivot_wider(c(geo, time), na_item, values_from = values) %>% 
  clean_names() %>% 
  mutate(tots = gross_domestic_product_at_market_prices + imports_of_goods_and_services,
         totd = final_consumption_expenditure_of_general_government + 
           household_and_npish_final_consumption_expenditure + exports_of_goods_and_services + gross_capital_formation,
         diff = tots - totd) -> analysis_data

analysis_data %>% 
  
  pivot_longer(-c(geo,time),names_to = "na_item",values_to = "values") %>% 
  mutate(group = case_when(na_item %in% c("totd","tots")~"Aggregate",
                           na_item %in% c("diff","statistical_discrepancy_expenditure_approach")~"Discrepancy",
                           na_item %in% c("final_consumption_expenditure_of_general_government",
                                          "household_and_npish_final_consumption_expenditure",
                                          "exports_of_goods_and_services","gross_capital_formation") ~ "Demand",
                           na_item %in% c("gross_domestic_product_at_market_prices","imports_of_goods_and_services")~"Supply",
                           TRUE ~ na_item)) %>% 
  
  
  ggplot(aes(x = time, y = values, color = na_item)) + 
  geom_line(size = 1) + 
  facet_wrap(~group, scales = "free_y")+
  theme_minimal()+
  theme(legend.position = "right") + 
  geom_hline(aes(yintercept = 0))+
  scale_color_viridis_d(name = "Variable") + 
  scale_y_continuous(labels = function(x){scales::dollar(x,scale = 1/1000,suffix = " Mrd. €")}) +
  labs(x = NULL, y = NULL) + 
  theme(panel.background = element_rect(fill = NA, colour = "grey")) -> a

  
 ggplotly(a)  
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
if(saving){write_csv(overall, "Quarterly GDP Components.csv")}